﻿using IBll;
using IDal;
using Model;
using System.Xml.Linq;
using System;
using Microsoft.VisualBasic;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace Bll
{
    public class MemberBll : IMemberBll
    {
        public MemberBll(IBaseDal<MemberLevel>memberlevelDal,IBaseDal<Member>memberDal)
        {
            MemberlevelDal = memberlevelDal;
            MemberDal = memberDal;
        }

        public IBaseDal<MemberLevel> MemberlevelDal { get; }
        public IBaseDal<Member> MemberDal { get; }

        public Task<int> AddMember(Member member)
        {
            member.CreateDate = DateTime.Now;
            string x = DateTime.Now.ToString("yyyymmdd");
            string y = new Random().Next(100,1000).ToString();
            member.Number = x + y;
            return MemberDal.AddAsync(member);
        }

        public List<MemberLevel> GetMemberLevel()
        {
            return MemberlevelDal.GetAsync().ToList();
        }

        public dynamic GetMember(string? name, string? levelId, DateTime? dateTime,int min,int max,int sex)
        {
            var member=MemberDal.GetAsync().Where(a=>a.IsDel==false);
            var memberlevel=MemberlevelDal.GetAsync();
            var m = from a in member
                    join b in memberlevel
                    on a.LevelId equals b.LevelId
                    select new
                    {
                        a.Id,
                        a.LevelId,
                        a.Number,
                        a.Name,
                        a.Tel,
                        a.Pwd,
                        a.Age,
                        a.ChuShengDate,
                        a.Sex,
                        a.State,
                        b.LevelName,
                        a.IsDel,
                        a.CreateUserId,
                        a.CreateDate,
                        a.DelUserId,
                        a.DelDate,
                        a.UpdaUserId,
                        a.UpdaDate
                       };
            if (!string.IsNullOrEmpty(name))
            {
                m = m.Where(x => x.Name.Contains(name));
            }
            if (!string.IsNullOrEmpty(levelId))
            {
                m = m.Where(x => x.LevelId.Equals(levelId));
            }
            if (dateTime != null)
            {
                var d=dateTime.Value.AddDays(1);
                m = m.Where(x => x.CreateDate >= dateTime && x.CreateDate < d);
            }
            if (min > 0)
            {
                m = m.Where(x => x.Age >= min);
            }
            if (max > 0)
            {
                m = m.Where(x => x.Age <= max);
            }
            if (sex > 0)
            {
                m = m.Where(x => x.Sex == sex);
            }
            return m.ToList();
        }

        public async Task<int> DelMember(int id)
        {
            var m = MemberDal.GetAsync().FirstOrDefault(x => x.Id == id);
            m.IsDel= true;
            m.CreateDate = DateTime.Now;
            return await MemberDal.UpdaAsync(m);
        }

        public Member FanTian(int id)
        {
            return MemberDal.GetAsync().FirstOrDefault(x => x.Id == id);
        }

        public async Task<int> UpdaMember(Member member)
        {
            member.UpdaDate = DateTime.Now;
            return await MemberDal.UpdaAsync(member);
        }
    }
}
